package com.um.psystem.service.exService.impl;

import com.um.psystem.mapper.platform.PublicMapper;
import com.um.psystem.model.vo.JsonResult;
import com.um.psystem.service.exService.IExceptDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: AiLen2E
 * @Date: 2021/10/27 10:31
 * @version: 1.0
 */
@Service
public class ExceptDictService implements IExceptDictService {

    @Autowired
    PublicMapper publicMapper;

    @Override
    public List<Map<String, Object>> getDeptList(Map map) {
        String sqltmp = "";
        String dept_name;
        Map paramsMap = new HashMap();
        Integer page = Integer.parseInt((map.get("page") != null ? map.get("page").toString() : "0"));
        Integer rows = Integer.parseInt((map.get("rows") != null ? map.get("rows").toString() : "0"));

        if (map.containsKey("dept_name") && (!map.get("dept_name").toString().equals(""))) {
            dept_name = map.get("dept_name").toString();
            paramsMap.put("dept_name", dept_name);
            sqltmp+=" WHERE dept_name=#{dept_name}";
        }

        String sql = "SELECT COUNT(*) as totals FROM ws_eng_exception_dept"+sqltmp;

        paramsMap.put("sqlStr", sql);
        int totals = 0;
        List<Map<String, Object>> list_count = publicMapper.getPublicItems(paramsMap);
        if (list_count.size() > 0) {
            totals = Integer.parseInt(list_count.get(0).get("totals").toString());
        } else {
            System.out.println("****************:" + totals);
        }

        sql = "SELECT a.*,"+totals+" as totals FROM ws_eng_exception_dept a"+sqltmp;

        if (page != 0 && rows != 0) {
            sql += " limit #{startIndex},#{rows} ";
            paramsMap.put("startIndex", (page - 1) * rows);
            paramsMap.put("rows", rows);
        }
        paramsMap.put("sqlStr", sql);
        return publicMapper.getPublicItems(paramsMap);
    }

    @Override
    public JsonResult<Integer> deleDept(Integer id) {

        Map paramsMap = new HashMap();
        String sql = "DELETE FROM ws_eng_exception_dept" +
                " WHERE" +
                " id=#{id}";
        paramsMap.put("dsqlStr",sql);
        paramsMap.put("id",id);
        Integer updateNum = publicMapper.delItems(paramsMap);
        return JsonResult.success(updateNum);

    }

    @Override
    public JsonResult<Integer> addDept(Map map) {

        Map paramsMap = new HashMap();
        String sql = "INSERT INTO ws_eng_exception_dept VALUES(NULL,#{dept_name},NOW(),#{remark})";
        paramsMap.put("isqlStr",sql);

        if (map.get("dept_name")!=null && !map.get("dept_name").equals(""))
        { paramsMap.put("dept_name",map.get("dept_name").toString()); }
        else
        { return JsonResult.error(400,"部门名称格式错误！"); }

        paramsMap.put("remark",map.get("remark"));
        Integer updateNum = publicMapper.saveItems(paramsMap);
        return JsonResult.success(updateNum);
    }

    @Override
    public JsonResult<Integer> editDept(Map map) {

        Map paramsMap = new HashMap();

        if (map.get("id")==null || map.get("id").equals(""))
        { return JsonResult.error(403,"非法请求"); }
        if (map.get("dept_name")==null || map.get("dept_name").equals("") || map.get("remark")==null)
        { return JsonResult.error(400,"参数错误"); }
        String id = map.get("id").toString();
        String dept_name = map.get("dept_name").toString();
        String remark = map.get("remark").toString();

        String sql = "UPDATE ws_eng_exception_dept SET dept_name=#{dept_name},remark=#{remark} WHERE id=#{id}";
        paramsMap.put("usqlStr", sql);
        paramsMap.put("dept_name", dept_name);
        paramsMap.put("remark", remark);
        paramsMap.put("id", id);

        return JsonResult.success(publicMapper.updateItems(paramsMap));

    }

    @Override
    public List<Map<String, Object>> getExceptionTypeList(Map map) {
        String sqltmp = "";
        String exception_type_name;
        Map paramsMap = new HashMap();
        Integer page = Integer.parseInt((map.get("page") != null ? map.get("page").toString() : "0"));
        Integer rows = Integer.parseInt((map.get("rows") != null ? map.get("rows").toString() : "0"));

        if (map.containsKey("exception_type_name") && (!map.get("exception_type_name").toString().equals(""))) {
            exception_type_name = map.get("exception_type_name").toString();
            paramsMap.put("exception_type_name", exception_type_name);
            sqltmp+=" WHERE exception_type_name=#{exception_type_name}";
        }

        String sql = "SELECT COUNT(*) as totals FROM ws_eng_exception_type"+sqltmp;

        paramsMap.put("sqlStr", sql);
        int totals = 0;
        List<Map<String, Object>> list_count = publicMapper.getPublicItems(paramsMap);
        if (list_count.size() > 0) {
            totals = Integer.parseInt(list_count.get(0).get("totals").toString());
        } else {
            System.out.println("****************:" + totals);
        }

        sql = "SELECT a.*,"+totals+" as totals FROM ws_eng_exception_type a"+sqltmp;

        if (page != 0 && rows != 0) {
            sql += " limit #{startIndex},#{rows} ";
            paramsMap.put("startIndex", (page - 1) * rows);
            paramsMap.put("rows", rows);
        }
        paramsMap.put("sqlStr", sql);
        return publicMapper.getPublicItems(paramsMap);
    }

    @Override
    public JsonResult<Integer> deleExceptionType(Integer id) {

        Map paramsMap = new HashMap();
        String sql = "DELETE FROM ws_eng_exception_type" +
                " WHERE" +
                " id=#{id}";
        paramsMap.put("dsqlStr",sql);
        paramsMap.put("id",id);
        Integer updateNum = publicMapper.delItems(paramsMap);
        return JsonResult.success(updateNum);
    }

    @Override
    public JsonResult<Integer> addExceptionType(Map map) {
        Map paramsMap = new HashMap();
        String sql = "INSERT INTO ws_eng_exception_type VALUES(NULL,#{exception_type_name},NOW())";
        paramsMap.put("isqlStr",sql);

        if (map.get("exception_type_name")!=null && !map.get("exception_type_name").equals(""))
        {
            paramsMap.put("exception_type_name",map.get("exception_type_name").toString());
        }else
        {
            return JsonResult.error(400,"参数格式错误！");
        }
        Integer updateNum = publicMapper.saveItems(paramsMap);
        return JsonResult.success(updateNum);
    }

    @Override
    public JsonResult<Integer> editExceptionType(Map map) {
        Map paramsMap = new HashMap();

        if (map.get("id")==null || map.get("id").equals(""))
        { return JsonResult.error(403,"非法请求");}
        if (map.get("exception_type_name")==null || map.get("exception_type_name").equals(""))
        { return JsonResult.error(400,"参数错误");}

        String id = map.get("id").toString();
        String exception_type_name = map.get("exception_type_name").toString();

        String sql = "UPDATE ws_eng_exception_type SET exception_type_name=#{exception_type_name} WHERE id=#{id}";
        paramsMap.put("usqlStr", sql);
        paramsMap.put("exception_type_name", exception_type_name);
        paramsMap.put("id", id);

        return JsonResult.success(publicMapper.updateItems(paramsMap));
    }
}
